﻿using Microsoft.Extensions.Configuration;

namespace Md.Api.DDD;
/// <summary>
/// 获取JSON配置文件帮助类
/// </summary>
public class JsonConfig
{
    /// <summary>
    /// 获取环境变量
    /// </summary>
    private static string? env = Environment.GetEnvironmentVariable("Environment");
    /// <summary>
    /// 根据节点名称和属性获取值
    /// </summary>
    /// <param name="key">键</param>
    /// <returns>string</returns>
    public static string JsonRead(string key)
    {
        var builder = new ConfigurationBuilder();

        builder.SetBasePath(Directory.GetCurrentDirectory());

        if (env == null)
        {
            builder.AddJsonFile($"appsettings.json");
        }
        else
        {
            builder.AddJsonFile($"appsettings.{env}.json");
        }

        var config = builder.Build();

        return config.GetSection(key).Value;
    }
}

